//
//  ONEChatContactViewController.m
//  ONEChat
//
//  Created by 楼楚 on 15/12/23.
//  Copyright © 2015年 Chu Lou. All rights reserved.
//

#import "ONEChatContactViewController.h"
#import "ONEChatChatViewController.h"

@interface  ONEChatContactViewController ()<NSFetchedResultsControllerDelegate>
{
    NSFetchedResultsController *_resultsControl;
}
@property (nonatomic ,strong) NSArray *friends;

@end

@implementation ONEChatContactViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadfriend];
}

- (void) loadfriend
{
    NSManagedObjectContext *context = [ONEChatXMPPTool sharedONEChatXMPPTool].rosterStorge.mainThreadManagedObjectContext;
    
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"XMPPUserCoreDataStorageObject"];
    
    NSString *jid = [ONEChatUserInfo sharedONEChatUserInfo].jid;
    
    
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"streamBareJidStr = %@",jid];
    request.predicate = pre;
    
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"displayName" ascending:YES];
    request.sortDescriptors= @[sort];
    
    
    _resultsControl = [[NSFetchedResultsController alloc]initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
    _resultsControl.delegate = self;
    NSError *err = nil;
    
    [_resultsControl performFetch:&err];
    if (err) {
        NSLog(@"%@",err);
    }
}
#pragma mark - 数据内容改变时调用的方法
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    NSLog(@"changeeeee");
    //    刷新表格
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _resultsControl.fetchedObjects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID =@"contactcell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    XMPPUserCoreDataStorageObject *friend = _resultsControl.fetchedObjects[indexPath.row];
    switch ([friend.sectionNum intValue]) {
        case 0:
            cell.detailTextLabel.text = @"在线";
            break;
        case 1:
            cell.detailTextLabel.text = @"离开";
            break;
        case 2:
            cell.detailTextLabel.text = @"离线";
            break;
            
        default:
            break;
    }
    cell.textLabel.text = friend.jidStr;
    
    return cell;
}
#pragma mark - 删除好友
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除好友");
        XMPPUserCoreDataStorageObject *friend = _resultsControl.fetchedObjects[indexPath.row];
        XMPPJID *friendjid = friend.jid;
        [[ONEChatXMPPTool sharedONEChatXMPPTool].roster removeUser:friendjid];
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    获取好友
    XMPPUserCoreDataStorageObject *friend =_resultsControl.fetchedObjects[indexPath.row];
    //    选中表格进行聊天界面
    [self performSegueWithIdentifier:@"ChatSegue" sender:friend.jid];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    id destVc = segue.destinationViewController;
    if ([destVc isKindOfClass:[ONEChatChatViewController class]]) {
        ONEChatChatViewController *chatVc = destVc;
        chatVc.friendjid = sender;
        
    }
}
@end
